package com.tools.web.work.obs;

import com.obs.services.ObsClient;
import com.obs.services.model.*;
import com.tools.common.container.OwnPairs;
import com.tools.common.object.Note;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * OBS 对象存储服务的操作实体类
 * 一般设置为单例使用，配置成 Spring Bean 来管理
 * */
@Note("OBS 对象存储服务的操作实体类" +
        "一般设置为单例使用，配置成 Spring Bean 来管理")
public final class OBSClient {

    @Note("OBS 客户端实例")
    private final ObsClient client;

    /* **************************************************************************************
     *
     *          构造器
     *
     * **************************************************************************************
     * */

    public OBSClient(OBSSource source) {
        if(source == null) throw new NullPointerException("OBS 客户端实例构建所需的参数项为 null");
        this.client = OBSKit.newObsClient(source);
    }

    public OBSClient(ObsClient client) {
        if(client == null) throw new NullPointerException("OBS 客户端实例不能为 null");
        this.client = client;
    }

    /* **************************************************************************************
     *
     *          操作方法
     *
     * **************************************************************************************
     * */

    public PutObjectResult createDir(String bucketName, String dirName) {
        return OBSKit.createDir(client, bucketName, dirName);
    }

    public PutObjectResult upload(OBSBuilder builder) {
        return OBSKit.upload(client, builder);
    }

    public List<PutObjectResult> uploads(OBSBuilder... builders) {
        return OBSKit.uploads(client, builders);
    }

    public List<PutObjectResult> uploads(Iterable<OBSBuilder> builders) {
        return OBSKit.uploads(client, builders);
    }

    public AppendObjectResult appendUpload(OBSBuilder builder, int appendPosition) {
        return OBSKit.appendUpload(builder, appendPosition);
    }

    public CompleteMultipartUploadResult segmentUploadNotContinue(OBSBuilder builder) {
        return OBSKit.segmentUploadNotContinue(client, builder);
    }

    public CompleteMultipartUploadResult segmentUploadIsContinue(OBSBuilder builder) {
        return OBSKit.segmentUploadIsContinue(client, builder);
    }

    public List<ListPartsResult> listSegments(OBSBuilder builder, String uploadID, int offset, int pageSize) {
        return OBSKit.listSegments(client, builder, uploadID, offset, pageSize);
    }

    public List<MultipartUpload> listSegmentsUploadTask(String bucketName) {
        return OBSKit.listSegmentsUploadTask(client, bucketName);
    }

    public ObjectMetadata getObjectMetaData(OBSBuilder builder) {
        return OBSKit.getObjectMetaData(client, builder);
    }

    public ObsObject getObject(OBSBuilder builder) {
        return OBSKit.getObject(client, builder);
    }

    public DownloadFileResult segmentDownloadIsContinue(OBSBuilder builder, String downloadFilePath) {
        return OBSKit.segmentDownloadIsContinue(client, builder, downloadFilePath);
    }

    public InputStream download(String bucketName, String objectKey) {
        return OBSKit.download(client, bucketName, objectKey);
    }

    public DeleteObjectResult deleteFile(String bucketName, String objectKey) {
        return OBSKit.deleteFile(client, bucketName, objectKey);
    }

    public DeleteObjectsResult deleteFiles(String bucketName, String... keys) {
        return OBSKit.deleteFiles(client, bucketName, keys);
    }

    public DeleteObjectsResult deleteFiles(String bucketName, Iterable<String> keys) {
        return OBSKit.deleteFiles(client, bucketName, keys);
    }

    public boolean fileExists(String bucketName, String objectKey) {
        return OBSKit.fileExists(client, bucketName, objectKey);
    }

    public ObjectMetadata setMetaData(OBSBuilder builder, Map headers) {
        return OBSKit.setMetaData(client, builder, headers);
    }

    public ObjectMetadata setMetaData(OBSBuilder builder, OwnPairs headers) {
        return OBSKit.setMetaData(client, builder, headers);
    }

    public ObjectListing listObjects(String bucketName, String prefix, String delimiter, String offset, int pageSize) {
        return OBSKit.listObjects(client, bucketName, prefix, delimiter, offset, pageSize);
    }

    public void close() {
        OBSKit.clientClose(client);
    }
}
